package com.tonghe.dao;

import com.tonghe.pojo.User;
import com.tonghe.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date:2021/12/13- 12- 13- 22:13
 **/

public class UserDao {

    @Test
    public void testGetListLike1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserLike1("%003%");//java代码执行的时候传递通配符 % %
        for (User user:userList) {
            System.out.println(user.toString());
        }
    }

    @Test
    public void testGetListLike2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserLike2("003");//再sql拼接中使用通配符
        for (User user:userList) {
            System.out.println(user.toString());
        }
    }

    @Test
    public void testGetList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();
        for (User user:
             userList) {
            System.out.println(user);
        }
    }
    @Test
    public void testById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);

        sqlSession.close();
    }
    @Test
    public void testAddUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rs = mapper.addUser(new User(3, "user003", "password003"));
        if(rs>0) sqlSession.commit();

        sqlSession.close();
    }

    @Test
    public void testAdd2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("id",3);
        map.put("name","username003");
        map.put("userpassword","userpassword003");
        int rs = mapper.addUser2(map);
        if(rs > 0){
            sqlSession.commit();
        }
        sqlSession.close();
    }

    @Test
    public void testUpdUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rs = mapper.updateUser(new User(3, "user0031", "password0031"));
        if(rs>0) sqlSession.commit();

        sqlSession.close();
    }

    @Test
    public void testDelUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int rs = mapper.deleteUser(3);
        if(rs>0) sqlSession.commit();

        sqlSession.close();
    }
}
